import os
import pytest
from common import TestYamlRead, TestRequests


log_path = os.path.dirname(os.path.abspath(os.path.abspath(__file__)))


# 读取config.yaml中的配置文件
def read_url(content):
    url_path = os.path.join(log_path, r"config.yaml")
    return TestYamlRead.yaml_read(url_path)[content]


# 读取yaml文件中的测试用例
def read_case(file):
    case_folder = os.path.join(log_path, r"case_data\log_in")
    data_path = os.path.join(case_folder, file)
    return TestYamlRead.yaml_read(data_path)


@pytest.fixture()
def front_after():
    print("————————————————————————开始执行用例————————————————————————")
    data = read_url("log_in")
    run = TestRequests.HttpPack().http(data["url_name"] + data["log_api"],
                                       method=data["method"], data=data["log_requests"])
    log_in_token = {"x-auth-token": run["b"]["login_token"]}
    yield log_in_token
    print("————————————————————————用例执行结束————————————————————————")


# @pytest.hookimpl(hookwrapper=True)
# def pytest_runtest_makereport(item, call):
#     start_time = datetime.now()
#     outcome = yield
#     report = outcome.get_result()
#     duration = (datetime.now() - start_time).total_seconds()
#
#     logger = logging.getLogger(item.name)
#     if report.when == "call":
#         if report.passed:
#             logger.info(f"用例执行通过 | 执行时间: {duration}s")
#         elif report.failed:
#             logger.error(f"用例执行失败 | 执行时间: {duration}s | Error: {report.longreprtext}")